﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using SLS.ExClassLib.RTIService;
using SLS.DataBase;
using SLS.ExClassLib.Data;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Helpers;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.MVVM;
namespace SLS.Base
{
    public partial class ExEDIT : SLBC
    { 
        public override void SLBC_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this as DependencyObject))
            {
                return;
            }  
            base.SLBC_Loaded(sender, e);
            Load5ExchangeDataSet(SetUpdoc);
            # region Set up Custom Tracking
            /*
                        RegisterMetadata();
                        AddWorkflowDesigner();
                        AddTrackingTextbox(); 
                        WorkflowInspectionServices.CacheMetadata(WFActivity);
                        //Mapping between the Object and Line No.
                        wfElementToSourceLocationMap = UpdateSourceLocationMappingInDebuggerService();

                        //Mapping between the Object and the Instance Id
                        activityIdToWfElementMap = BuildActivityIdToWfElementMap(wfElementToSourceLocationMap);
                        const String all = "*";
                        Tracker = new VisualTrackingParticipant()
                        {
                            TrackingProfile = new TrackingProfile()
                            {
                                Name = "CustomTrackingProfile",
                                Queries = 
                        {
                            new CustomTrackingQuery() 
                            {
                                Name = all,
                                ActivityName = all
                            },
                            new WorkflowInstanceQuery()
                            {
                                // Limit workflow instance tracking records for started and completed workflow states
                                States = { WorkflowInstanceStates.Started, WorkflowInstanceStates.Completed },
                            },
                            new ActivityStateQuery()
                            {
                                // Subscribe for track records from all activities for all states
                                ActivityName = all,
                                States = { all },

                                // Extract workflow variables and arguments as a part of the activity tracking record
                                // VariableName = "*" allows for extraction of all variables in the scope
                                // of the activity
                                Variables = 
                                {                                
                                    { all }   
                                }
                            }   
                        }
                            }
                        };
                        Tracker.ActivityIdToWorkflowElementMap = activityIdToWfElementMap;
                        //As the tracking events are received
                        Tracker.TrackingRecordReceived += new EventHandler<TrackingEventArgs>(simTracker_TrackingRecordReceived);
           ThreadPool.QueueUserWorkItem(new WaitCallback((context) =>
            {
                //This is to remove the final debug adornment
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Render
                    , (Action)(() =>
                    {
                        WorkflowDesigner.DebugManagerView.CurrentLocation = new SourceLocation(WorkflowXAML, 1, 1, 1, 10);
                    }));

            }));
           ViewDataSet.WFMachine.Tracker = Tracker;
                        */
            # endregion
            this.ViewDataSet.WFActivity = WFActivity;
            ViewDataSet.WFMachine = new WFMachine(Address, ViewDataSet, WFActivity);
            this.ViewDataSet.ExecutePowerOn(flagStartQueryUI);
        } 
 
        public override void HandleActiveDataRowChanged(DataRow dwTemp)
        {
            base.HandleActiveDataRowChanged(dwTemp);
            try
            {
                ExDataRow exd = new ExDataRow();
                ExDataRow dwThis = exd.GetExDataRow(dwTemp, false);
                if (CheckGoodsMasterKeyRelation)
                {
                    ValidateGDKEY(dwThis.Item);
                }
                if (!flagLoadForeignKeyTable)
                    return; 
                if (ValidateGoodsMasterKeyRelation)
                {
                    if (!dwThis.SUBSTATUS.Contains("GoodsMasterKeyRelationComplete;"))
                        return;
                }
                MessageEx msg = new MessageEx(Address, "UIFederation", "OnLoadForeignKeyTable", "YES");
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                //EventDispatcher.Instance.POSTUI2UIMessage(msg, dwThis);
                object data = dwThis;
                EventDispatcher.Instance.Publish(msg, data);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public virtual void LoadForeignKeyTable(DataRow dwThis, MessageEx msgA)
        {
            try
            {
                SLDBSET setThis = SetUI;
                Tracer.Info("Active data in GoodsMasterProperty".PadLeft(20, '-'), dwThis);
                using (new LongOperation(this))
                {
                    Mouse.SetCursor(Cursors.Wait);
                    if (dwThis == null)
                        return;
                    ExDataRow exdwThis = null;
                    string INSTANCEID = dwThis["INSTANCEID"].ToString();
                    dwThis = this.ViewDataSet.GetRecord(INSTANCEID);
                    if (dwThis == null)
                        return;
                    ViewDataSet.Clear(dwThis);
                    MessageEx msgB;
                    if (false)
                    {
                        msgB = EventDispatcher.Instance.MessagePool.Reply(msgA.MessageID, "UIFederation", "OnSaveActiveRecord", "YES");
                        msgB.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msgB);
                        msgB.CallerUID = dwThis["UID"].ToString();
                        object data = dwThis;
                        EventDispatcher.Instance.Publish(msgB, data);
                        //EventDispatcher.Instance.POSTUI2UIMessage(msgB, dwThis);
                    }
                    if (flagLoadSet5KeyDef)
                    {
                        msgB = EventDispatcher.Instance.MessagePool.Reply(msgA.MessageID, "UIFederation", "OnLoadSet5KeyDef", "YES");
                        msgB.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msgB);
                        msgB.Address = Address;
                        msgB.CallerUID = dwThis["UID"].ToString();
                        object data = dwThis;
                        EventDispatcher.Instance.Publish(msgB, data);
                        //EventDispatcher.Instance.POSTUI2UIMessage(msgB, dwThis);
                    }
                    if (flagLoadSet4Reference)
                    {
                        msgB = EventDispatcher.Instance.MessagePool.Reply(msgA.MessageID, "UIFederation", "OnLoadSet4Reference", "YES");
                        msgB.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msgB);
                        msgB.CallerUID = dwThis["UID"].ToString();
                        object data = dwThis;
                        EventDispatcher.Instance.Publish(msgB, data);
                        //EventDispatcher.Instance.POSTUI2UIMessage(msgB, dwThis);
                    }
                    if (flageLoadSet2Create)
                    {
                        msgB = EventDispatcher.Instance.MessagePool.Reply(msgA.MessageID, "UIFederation", "OnLoadSet2Create", "YES");
                        msgB.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msgB);
                        msgB.CallerUID = dwThis["UID"].ToString();
                        object data = dwThis;
                        EventDispatcher.Instance.Publish(msgB, data);
                        //EventDispatcher.Instance.POSTUI2UIMessage(msgB, dwThis);
                    }
                    if (false)
                    {
                        msgB = EventDispatcher.Instance.MessagePool.Reply(msgA.MessageID, "UIFederation", "OnSaveActiveRecord", "YES");
                        msgB.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msgB);
                        msgB.CallerUID = dwThis["UID"].ToString();
                        object data = dwThis;
                        EventDispatcher.Instance.Publish(msgB, data);
                        //EventDispatcher.Instance.POSTUI2UIMessage(msgB, dwThis);
                    }
                }
                Mouse.SetCursor(Cursors.Arrow);
            }
            catch (Exception ex)
            {
                throw;
            }
        } 

        public void LoadCreatedRecord(DataSet set2Create, string InstanceID)
        {
            DataSet setEvent;
            string TBName = "";
            ExDataRow dwThis = new ExDataRow();
            try
            {
                foreach (DataTable tbTemp in set2Create.Tables)
                {
                    dwThis =ExRowBase.MergeDataList(InstanceID, tbTemp, "Set2Create");
                }
                if (dwThis != null)
                {
                    dwThis.CheckingSet2Create(true);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        } 
    }
}
